package apis

import (
	"errors"
	"fmt"
	"github.com/gin-gonic/gin"
	"vrcm/src/vrcm/app/admin/models/subject"

	//"github.com/go-admin-team/go-admin-core/sdk/api"
	_ "github.com/go-admin-team/go-admin-core/sdk/pkg/response"
	"sort"
	"vrcm/src/vrcm/common/apis"

	"vrcm/src/vrcm/app/admin/service/dto/subject"
	"vrcm/src/vrcm/app/admin/service/subject"
)

type Subject struct {
	apis.Api
}

// GetPage 获取Subject列表
// @Summary 获取Subject列表
// @Description 获取Subject列表
// @Tags Subject
// @Param pageSize query int false "页条数"
// @Param pageIndex query int false "页码"
// @Success 200 {object} response.Response{data=response.Page{list=[]models.Subject}} "{"code": 200, "data": [...]}"
// @Router /api/v1/subject [get]
// @Security Bearer
func (e Subject) GetPage(c *gin.Context) {
	req := dto.SubjectGetPageReq{}
	s := service.Subject{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	subjectWithOptions := make([]*subject.SubjectWithOptions, 0)
	list := make([]subject.Subject, 0)
	var count int64

	//获取题目
	err = s.GetPage(&req, &list, &count)
	if err != nil {
		e.Error(10001, err, fmt.Sprintf("获取Subject失败，\r\n失败信息 %s", err.Error()))
		return
	}
	ids := make([]int, 0, 15)
	for _, v := range list {
		ids = append(ids, v.Id)
	}

	//获取选项
	options, _ := s.GetSubjectOptions(ids)
	for _, v := range list {
		v := v
		subjectWithOption := &subject.SubjectWithOptions{}
		tempData := s.Filter(*options, func(t subject.SubjectOption) bool {
			return t.SubjectId == v.Id
		})
		//根据sort排序
		sort.Slice(tempData, func(i, j int) bool {
			return i < j
		})
		subjectWithOption.Subject = v
		subjectWithOption.Options = append(subjectWithOption.Options, tempData...)
		subjectWithOptions = append(subjectWithOptions, subjectWithOption)

	}

	e.PageOK(subjectWithOptions, int(count), req.GetPageIndex(), req.GetPageSize(), "查询成功")
}

// Get 获取Subject
// @Summary 获取Subject
// @Description 获取Subject
// @Tags Subject
// @Param id path int false "id"
// @Success 200 {object} response.Response{data=models.Subject} "{"code": 200, "data": [...]}"
// @Router /api/v1/subject/{id} [get]
// @Security Bearer
func (e Subject) Get(c *gin.Context) {
	req := dto.SubjectGetReq{}
	s := service.Subject{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	data, err := s.Get(&req)
	if err != nil || data == nil {
		e.Error(10001, err, fmt.Sprintf("获取Subject失败，\r\n失败信息 %s", err.Error()))
		return
	}

	options, err := s.GetSubjectOptions([]int{data.Id})
	if err != nil {
		e.Error(10001, err, fmt.Sprintf("获取SubjectOption失败，\r\n失败信息 %s", err.Error()))
		return
	}

	subjectWithOptions := subject.SubjectWithOptions{}
	subjectWithOptions.Subject = *data
	subjectWithOptions.Options = *options
	e.OK(subjectWithOptions, "查询成功")
}

// Insert 创建Subject
// @Summary 创建Subject
// @Description 创建Subject
// @Tags Subject
// @Accept application/json
// @Product application/json
// @Param data body dto.SubjectInsertReq true "data"
// @Success 200 {object} response.Response	"{"code": 200, "message": "添加成功"}"
// @Router /api/v1/subject [post]
// @Security Bearer
func (e Subject) Insert(c *gin.Context) {
	req := dto.SubjectInsertReq{}
	s := service.Subject{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	//数据校验
	if req.SubjectType == 1 && len(req.TitleImg) > 0 {
		err = errors.New("题目图片应为空")
		e.Error(500, err, fmt.Sprintf("创建Subject失败，\r\n失败信息 %s", err.Error()))
		return
	}

	err = s.Insert(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("创建Subject失败，\r\n失败信息 %s", err.Error()))
		return
	}

	e.OK(req.GetId(), "创建成功")
}

// Update 修改Subject
// @Summary 修改Subject
// @Description 修改Subject
// @Tags Subject
// @Accept application/json
// @Product application/json
// @Param id path int true "id"
// @Param data body dto.SubjectUpdateReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "修改成功"}"
// @Router /api/v1/subject/{id} [put]
// @Security Bearer
func (e Subject) Update(c *gin.Context) {
	req := dto.SubjectUpdateReq{}
	s := service.Subject{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	//数据校验
	if req.SubjectType == 1 && len(req.TitleImg) > 0 {
		err = errors.New("题目图片应该为空")
		e.Error(500, err, fmt.Sprintf("创建Subject失败，\r\n失败信息 %s", err.Error()))
		return
	}

	err = s.Update(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("修改Subject失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(req.GetId(), "修改成功")
}

// Delete 删除Subject
// @Summary 删除Subject
// @Description 删除Subject
// @Tags Subject
// @Param data body dto.SubjectDeleteReq true "body"
// @Success 200 {object} response.Response	"{"code": 200, "message": "删除成功"}"
// @Router /api/v1/subject [delete]
// @Security Bearer
func (e Subject) Delete(c *gin.Context) {
	s := service.Subject{}
	req := dto.SubjectDeleteReq{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	err = s.Remove(&req)
	if err != nil {
		e.Error(500, err, fmt.Sprintf("删除Subject失败，\r\n失败信息 %s", err.Error()))
		return
	}
	e.OK(req.GetId(), "删除成功")
}

func (e Subject) GetByPaperId(c *gin.Context) {
	req := dto.SubjectGetByPaperIdReq{}
	s := service.Subject{}
	err := e.MakeContext(c).
		MakeOrm().
		Bind(&req).
		MakeService(&s.Service).
		Errors
	if err != nil {
		e.Logger.Error(err)
		e.Error(500, err, err.Error())
		return
	}
	data, err := s.GetByPaperId(&req)
	if err != nil {
		e.Error(10001, err, fmt.Sprintf("获取Subject失败，\r\n失败信息 %s", err.Error()))
		return
	}

	//options, err := s.GetSubjectOptions([]int{data.Id})
	//if err != nil {
	//	e.Error(500, err, fmt.Sprintf("获取SubjectOption失败，\r\n失败信息 %s", err.Error()))
	//	return
	//}
	//
	//subjectWithOptions := models.SubjectWithOptions{}
	//subjectWithOptions.Subject = *data
	//subjectWithOptions.Options = *options
	e.OK(data, "查询成功")
}
